#include "mainwindow.h"

MainWindow::MainWindow():
            m_dispatcher(),
            m_dispatcherWebClient(),
            m_worker(),
            m_BoxMain(Gtk::Orientation::VERTICAL, 0),
            m_GridUp(),
            m_GridDown (),
            m_BoxPlot(Gtk::Orientation::HORIZONTAL,0),
            m_Button1("On"),
            m_Button2("TEST2"),
            m_Button3("TEST3"),
            m_Button4("TEST4"),
            m_Button5("TEST5"),
            m_Button6("TEST6"),
            m_Button7("TEST7"),
            m_Button8("TEST8"),
            m_Button9("TEST9"),
            m_Button10("TEST10"),
            m_ButtonTest ("STOP"),
            m_FrameLeft(),
            m_AFrame ()
{
    m_dispatcher.connect(sigc::mem_fun(*this,&MainWindow::OnNotification));
    m_dispatcherWebClient.connect(sigc::mem_fun(*this, &MainWindow::OnNotificationWebClient));
    m_thread = new boost::thread (
        [this]
        {
            m_worker.DoWork(this);
        });
 //   m_webclient = Gtk::make_managed<WebClient>();
}

void MainWindow::ButtonTestClicked()
{
    m_run_en = !m_run_en;
    Plot->PlotDraw("1",200,0,0,0);
   if (m_run_en)  {m_ButtonTest.set_label("RUN");} else m_ButtonTest.set_label("OFF");
  //  boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
   //  std::cout << "Time= " << start << std::endl;
   // std::cout << "Run_en= " << m_run_en << std::endl;
}

void MainWindow::Notify() {
    m_dispatcher.emit();
}

void MainWindow::OnNotification() {
    Plot = Gtk :: make_managed<Plot2D>();
    m_FrameLeft.set_size_request(600,600);
    m_AFrame.set_size_request(400,600);
    m_BoxMain.append(m_GridUp);
    m_BoxMain.append(m_BoxPlot);
    m_BoxMain.append(m_GridDown);
    m_GridUp.attach(m_Button1,0,0);
    m_GridDown.attach(m_ButtonTest ,0,0);
    m_GridDown.attach(m_Button2,1,0);
    m_GridDown.attach(m_Button3,2,0);
    m_GridDown.attach(m_Button4,3,0);
    m_GridDown.attach(m_Button5,4,0);
    m_GridDown.attach(m_Button6,5,0);
    m_GridDown.attach(m_Button7,6,0);
    m_GridDown.attach(m_Button8,7,0);
    m_GridDown.attach(m_Button9,8,0);
    m_GridDown.attach(m_Button10,9,0);
    m_BoxPlot.append(m_FrameLeft);
    m_BoxPlot.append(m_AFrame);
    set_child(m_BoxMain);
    Plot->AddDataToPlot(m_worker.GetPlotData());
    Plot->AddDataToPlot(m_worker.GetPlotData1());
    m_AFrame.set_child(*Plot);
    set_title("CEX");
    set_resizable(false);
    m_ButtonTest.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::ButtonTestClicked));
    m_Button2.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button2Clicked));
    m_Button3.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button3Clicked));
    m_Button4.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button4Clicked));
    m_Button5.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button5Clicked));
    m_Button6.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button6Clicked));
    m_Button7.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button7Clicked));
    m_Button8.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button8Clicked));
    m_Button9.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button9Clicked));
    m_Button10.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Button10Clicked));
}

void MainWindow::Button2Clicked()
{
Plot->PlotDraw("2",150,250,0.5,0.5);
}

void MainWindow::Button3Clicked()
{
Plot->PlotDraw("2",150,250,999,999);
}

void MainWindow::Button4Clicked()
{
//auto const target = "/v5/market/tickers?category=spot&symbol=BTCUSDT";
//m_webclient -> AsyncRequest( target);
//std::string subscription_message = R"({"op": "subscribe", "args": ["orderbook.50.BTCUSDT"]})";
//std::string subscription_message1 = R"({"op": "subscribe", "args": ["orderbook.1.BTCUSDT"]})";
//m_webclient ->test("stream-testnet.bybit.com","/v5/public/spot", subscription_message);
//m_webclient ->RunRequestLoop("stream-testnet.bybit.com","/v5/public/spot", subscription_message);
//m_webclient ->RunRequestLoop("stream-testnet.bybit.com","/v5/public/spot", subscription_message1);
}

void MainWindow::Button5Clicked()
{
//m_webclient->RunRequestLoop();
}

void MainWindow::Button6Clicked()
{
//m_webclient->StopRequestLoop();
}

void MainWindow::Button7Clicked()
{
coin_btc = boost::make_shared<Coin>("BTCUSDT");
coin_btc->Show();
coin_btc -> AddPublicStream();

}

void MainWindow::Button8Clicked()
{
coin_btc->StopWebSocket();
}

void MainWindow::Button9Clicked()
{

}

void MainWindow::Button10Clicked()
{

}

void MainWindow::OnNotificationWebClient() {

}

